#! /usr/bin/env python3

import json
import sys

# check argv
if len(sys.argv) != 4:
    print("Usage: python merge_json.py file1.json file2.json output_merged.json")
    sys.exit(1)

# read JSON file
with open(sys.argv[1], 'r') as f1, open(sys.argv[2], 'r') as f2:
    data1 = json.load(f1)
    data2 = json.load(f2)

# merge JSON data
if isinstance(data1, list) and isinstance(data2, list):
    merged_data = data1 + data2  # 合并为列表
elif isinstance(data1, dict) and isinstance(data2, dict):
    merged_data = {**data1, **data2}  # 合并为字典
else:
    print("The JSON files do not match in structure.")
    sys.exit(1)

# output merged JSON
with open(sys.argv[3], 'w') as outfile:
    json.dump(merged_data, outfile, indent=2)

print("Merged JSON saved to merged.json")
